package emulator.GUI.keyboard;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.LayoutStyle.ComponentPlacement;

import emulator.GUI.PersistentFrame;

public class VicKeyboardFrame extends PersistentFrame {
//	private VicKeyboard keyboard;
	private int block_width = 50;
	private int block_height = 50;
	
	public VicKeyboardFrame(VicKeyboard keyboard)
	{
		super("Vic Keyboard");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//		this.keyboard = keyboard;
		
		Font button_font = new Font("SansSerif", Font.BOLD, 10);
		Insets default_button_frame = new Insets(0, 0, 0, 0);
	
		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);
		
		SequentialGroup lines_down = layout.createSequentialGroup();
		ParallelGroup lines_right = layout.createParallelGroup(GroupLayout.Alignment.CENTER);
		for (int i = 0; i < keyboard.getRows(); i++)
		{
			ParallelGroup line_hor = layout.createParallelGroup(GroupLayout.Alignment.CENTER);
			SequentialGroup line_flow = layout.createSequentialGroup();
			for (int j = 0; j < keyboard.getCols(i); j++)
			{
				VicKey key = keyboard.getKey(i,j);
				if (key.getPreSpace() > 0)
				{
					line_flow.addPreferredGap(
							ComponentPlacement.UNRELATED,
							(int)(block_width*key.getPreSpace()),
							Short.MAX_VALUE);
				}
				
				String caption = key.getCaption();
				JButton button = new JButton(formatButtonText(caption));
				button.setMargin(default_button_frame);
				button.setPreferredSize(
						new Dimension(
								(int)(block_width*key.getSize().getWidth()),
								(int)(block_height*key.getSize().getHeight())));
				button.setFont(button_font);
				button.addMouseListener(new VicMouseEvent(button, key));
				
				line_hor.addComponent(button, 
							GroupLayout.PREFERRED_SIZE,
							GroupLayout.DEFAULT_SIZE,
							GroupLayout.PREFERRED_SIZE);
				line_flow.addComponent(button, 
						GroupLayout.PREFERRED_SIZE,
						GroupLayout.DEFAULT_SIZE,
						GroupLayout.PREFERRED_SIZE);
			}
			lines_down.addGroup(line_hor);
			lines_right.addGroup(line_flow);
		}
		layout.setVerticalGroup(lines_down);
		layout.setHorizontalGroup(lines_right);
	}
	
	private String formatButtonText(String caption) {
		if (caption.contains("\n"))
		{
			String html_caption = "<html><center>";
			for (int i = 0; i < caption.length(); i++)
			{
				if (caption.charAt(i) == '\n')
				{
					html_caption += "<p>";
				}
				else
					html_caption += caption.charAt(i);
			}
			html_caption += "</center></html>";
			return html_caption;
		}
		return caption;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 8810260280513032012L;

}
